package com.shadow.demo.component.variable;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shadow.demo.common.bo.R;
import com.shadow.demo.common.enums.DictParentEnums;
import com.shadow.demo.data.BaseController;
import com.shadow.demo.data.bo.Query;
import com.shadow.demo.data.shiro.entity.Role;
import com.shadow.demo.data.variable.VariableEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 环境变量控制器.
 */
@RestController
@RequestMapping("/variable")
public class VariableController extends BaseController {

    private final VariableService variableService;

    VariableController(VariableService variableService) {
        this.variableService = variableService;
    }

    /**
     * 新增数据字典.
     * @author Administrator
     * @date 18:08 2020/9/8
     * @param dictionary 字典对象
     **/
    @PostMapping("/save")
    public R save(@RequestBody @Valid final VariableEntity dictionary) {
        variableService.saveDic(dictionary);
        return R.success("数据字典新增成功");
    }

    /**
     * 删除数据字典.
     * @author Administrator
     * @date 18:08 2020/9/8
     * @param params 参数map
     **/
    @PostMapping("/delete")
    public R delete(@RequestParam final Map<String, Object> params) {
        final String id = (String) params.get("id");
        if (StringUtils.isNotBlank(id)) {
            variableService.deleteDic(id);
        }
        return R.success("数据字典删除成功");
    }

    /**
     * 更新数据字典.
     * @author Administrator
     * @date 18:08 2020/9/8
     * @param dictionary 字典对象
     **/
    @PostMapping("/update")
    public R update(@RequestBody @Valid final VariableEntity dictionary) {
        dictionary.setModifyTime(LocalDateTime.now());
        variableService.updateDic(dictionary);
        return R.success("数据字典更新成功");
    }

    /**
     * 编辑数据字典页面.
     * @author Administrator
     * @date 18:08 2020/9/8
     * @param params 参数map
     * @param model 数据模型
     **/
    @GetMapping("/edit")
     public String edit(@RequestParam final Map<String, Object> params, final ModelMap model) {
        final VariableEntity dictionary = Optional.ofNullable((String) params.get("id"))
                .filter(StringUtils::isNotBlank)
                .map(x -> variableService.getById(x))
            .orElse(new VariableEntity());
        model.addAttribute("dictionaryForm", dictionary);
        setCommonModelMap(model);
        return "dictionary/edit.html";
    }



    /**
     * 获取字典数据列表.
     **/
    @PostMapping("/getDictList")
    public R getDictList(@RequestParam final Map<String, Object> params) {

        return R.success();
    }


    /**
     * 设置公共参数到前台页面.
     * @author pbody
     * @date 22:19 2022/11/1
     * @param modelMap 模板参数map
     **/
    private void setCommonModelMap(final ModelMap modelMap) {
        modelMap.addAttribute("parentList", variableService.getParentAuthList(true));
    }

    /**
     * 数据字典唯一性验证.
     * @author Administrator
     * @date 18:09 2020/9/8
     * @param params 参数map
     **/
    @PostMapping("/isExists")
    public R isExist(@RequestParam final Map<String, Object> params) {
        final String name = (String) params.get("name");
        final String value = (String) params.get("value");
        final String parentId = (String) params.get("parentId");
        if (StringUtils.isNotBlank(name) || StringUtils.isNotBlank(value) || StringUtils.isNotBlank(parentId)) {
            boolean isExist = variableService.isExists(name, value, parentId, (String) params.get("id"));
            return R.success().put("obj", isExist);
        }
        return R.fail("RStatusEnums.PARAM_IS_BLANK");
    }

    @PostMapping("/list")
    public R getUserList(@RequestParam Map<String, Object> params) {
        Page<VariableEntity> page = variableService.getList(params, true);
        return successPage(page);
    }
}
